//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Seasonality.h"
using namespace Cephei::QL::Termstructures::Inflation;
#include <gen/QL/Termstructures/InflationTermStructure.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (boost::shared_ptr<QuantLib::Seasonality>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSeasonality = NULL;
#endif
	_ppSeasonality = &childNative;
    
}
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (QuantLib::Seasonality& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSeasonality = NULL;
#endif
	_ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (&childNative);
    
    _SeasonalityOwner = owner;
    
}

Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (CSeasonality^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSeasonality = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSeasonality = NULL;
#endif
	if (!t->IsSubclassOf(CSeasonality::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (QuantLib::Handle<QuantLib::Seasonality>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSeasonality = &childNative;
	_ppSeasonality = &static_cast<boost::shared_ptr<QuantLib::Seasonality>>(childNative.currentLink());
    
    _SeasonalityOwner = owner;
}
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (QuantLib::Handle<QuantLib::Seasonality> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSeasonality = &childNative;
	_ppSeasonality = &static_cast<boost::shared_ptr<QuantLib::Seasonality>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Inflation::CSeasonality::CSeasonality (QuantLib::Seasonality childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSeasonality = NULL;
#endif
	_ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (new QuantLib::Seasonality (childNative));
    
}
#endif

Cephei::QL::Termstructures::Inflation::CSeasonality::~CSeasonality ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSeasonality != NULL)
    {
	    delete _ppSeasonality;
        _ppSeasonality = NULL;
    }
}
Cephei::QL::Termstructures::Inflation::CSeasonality::!CSeasonality ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSeasonality != NULL)
    {
	    delete _ppSeasonality;
    }
}
QuantLib::Seasonality& Cephei::QL::Termstructures::Inflation::CSeasonality::GetReference ()
{
    if (_ppSeasonality == NULL) throw REFNEW NativeNullException ();
	return **_ppSeasonality;
}
boost::shared_ptr<QuantLib::Seasonality>& Cephei::QL::Termstructures::Inflation::CSeasonality::GetShared ()
{
    if (_ppSeasonality == NULL) throw REFNEW NativeNullException ();
	return *_ppSeasonality;
}
QuantLib::Seasonality* Cephei::QL::Termstructures::Inflation::CSeasonality::GetPointer ()
{
    if (_ppSeasonality == NULL) throw REFNEW NativeNullException ();
	return &**_ppSeasonality;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Seasonality>& Cephei::QL::Termstructures::Inflation::CSeasonality::GetHandle ()
{
	if (_phSeasonality == NULL)
	{
		_phSeasonality = new Handle<QuantLib::Seasonality> (*_ppSeasonality);
	}
	return *_phSeasonality;
}
#endif
bool Cephei::QL::Termstructures::Inflation::CSeasonality::HasNative () 
{
	return (_ppSeasonality != NULL);
}

Double Cephei::QL::Termstructures::Inflation::CSeasonality::CorrectYoYRate (DateTime d, Double r, Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSeasonality)->correctYoYRate ( _d,  _r,  _iTS );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
Double Cephei::QL::Termstructures::Inflation::CSeasonality::CorrectZeroRate (DateTime d, Double r, Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSeasonality)->correctZeroRate ( _d,  _r,  _iTS );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
Boolean Cephei::QL::Termstructures::Inflation::CSeasonality::IsConsistent (Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	bool _rv = (bool)(*_ppSeasonality)->isConsistent ( _iTS );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

